<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<script th:src="@{/}+'js/tool.js'"></script>
  <style type="text/css">
	.layui-form .layui-form-item{
		width:90%;
	}
	.layui-input-inline{
		width:350px!important
	}
	</style>

</head>
<body style="margin-top: 100px">
<form class="layui-form" lay-filter="myform" id="myform">
  <input type="hidden" name="id" id="id">
  <div class="layui-form-item">
    <label class="layui-form-label">试卷名称</label>
    <div class="layui-input-inline">
      <input type="text" name="name" lay-verify="required" autocomplete="off" class="layui-input">
    </div>
  </div>
  <div class="layui-form-item">
    <label class="layui-form-label">语言</label>
    <div class="layui-input-inline">
      <select name="lg" id="lg"></select>
    </div>
   <label class="layui-form-label">考试科目</label>
    <div class="layui-input-inline">
      <input type="text" name="subject" autocomplete="off" class="layui-input">
    </div>
  </div>
  <div class="layui-form-item">
    <label class="layui-form-label">专业</label>
    <div class="layui-input-inline">
      <input type="text" id="majorName" name="majorName" autocomplete="off" class="layui-input" select>
      <input type="hidden" lay-verify="required" id="major" name="major">
    </div>
    <label class="layui-form-label">年级</label>
    <div class="layui-input-inline">
      <select lay-verify="required" name="grade" id="grade"></select>
    </div>
  </div>
  <div class="layui-form-item">
    <label class="layui-form-label">总题数</label>
    <div class="layui-input-inline">
      <input type="number" name="questionNum" id="questionNum" lay-verify="required" autocomplete="off" class="layui-input">
    </div>
    <label class="layui-form-label">难度</label>
    <div class="layui-input-inline">
      <select name="difficulty" id="difficulty"></select>
    </div>
  </div>
  <div class="layui-form-item">
    <label class="layui-form-label">单选题</label>
    <div class="layui-input-inline" style="width: 100px!important;">
      <input type="number" name="num1" id="num1" value="0" lay-verify="required" autocomplete="off" class="layui-input">
    </div>
    <label class="layui-form-label">多选题</label>
    <div class="layui-input-inline" style="width: 100px!important;">
      <input type="number" name="num2" id="num2" value="0" lay-verify="required" autocomplete="off" class="layui-input">
    </div>
    <label class="layui-form-label">判断题</label>
    <div class="layui-input-inline" style="width: 100px!important;">
      <input type="number" name="num3" id="num3" value="0" lay-verify="required" autocomplete="off" class="layui-input">
    </div>
    <label class="layui-form-label">简答题</label>
    <div class="layui-input-inline" style="width: 100px!important;">
      <input type="number" name="num4" id="num4" value="0" lay-verify="required" autocomplete="off" class="layui-input">
    </div>
    <label class="layui-form-label">论述题</label>
    <div class="layui-input-inline" style="width: 100px!important;">
      <input type="number" name="num5" id="num5" value="0" lay-verify="required" autocomplete="off" class="layui-input">
    </div>
  </div>
  <button id="submitBtn" class="layui-btn" style="display:none" lay-submit lay-filter="submitme">保存</button>
</form>
<div class="layui-row">
  <div class="layui-card-header">篇章设置</div>
  <table id="main-table" lay-filter="main-table"></table>
  <div id="treeTablePage"></div>
</div>
<script>
var categoryId = -1;
layui.link('layuiadmin/plugins/layui_complete-master/autocomplete.css'); 
layui.extend({
	autocomplete: '{/}layuiadmin/plugins/layui_complete-master/autocomplete' 
});
layui.extend({
  treeTable: '{/}layuiadmin/plugins/treeTable/treeTable'
});
var whr = {};
var limit = 20;
layui.use(['form', 'table', 'treeTable','autocomplete','laypage'], function(){
	  var form = layui.form
	  ,autocomplete = layui.autocomplete
    ,treeTable = layui.treeTable
    ,laypage = layui.laypage
	  ,table = layui.table;
    createSelect("paper-lg","lg",form,1,'请选择');
    createSelect("question-difficulty","difficulty",form,1,'请选择');
    createSelect("grade","grade",form,1,'请选择');
	setAutocomplete('lib/getPageList?page=1&limit=20',autocomplete,"libName",'{{d.name}}','{{d.name}}',"setLib");
	window.setLib = function(data){
		    $("#libId").val(data.id);
			$("#libName").val(data.name);
            categoryId = data.categoryId;
        insTb.refresh();
	}
  setAutocomplete('major/getPageList?page=1&limit=20',autocomplete,"majorName",'{{d.name}}','{{d.name}}',"setMajor");
  window.setMajor = function(data){
    $("#major").val(data.id);
    $("#majorName").val(data.name);
  }
  var insTb =treeTable.render({
    elem: '#main-table',
    tree: {
      iconIndex: 2,
      idName: 'id',       // 自定义id字段的名称
      pidName: 'pianId',
      isPidData: true, // 是否是pid形式数据
      openName: 'orderno'
    },
    checkboxCascade : 'none',
    cols: [
      {field: 'id',title: "id",hide:true},
      {field: 'pianId',title: "pianId",hide:true},
      {field: 'orderno', title: "排序",width:130},
      {field: 'name', title: "章节名称",width:280}
      ,{field: 'num1',title: "单选题",width:120,edit:'text'}
      ,{field: 'num2',title: "多选题",width:120,edit:'text'}
      ,{field: 'num3',title: "判断题",width:120,edit:'text'}
      ,{field: 'num4',title: "简答题",width:120,edit:'text'}
      ,{field: 'num5',title: "论述题",width:120,edit:'text'}
    ],
    height :"420px",
    done:function(){


    },
    reqData: function (data, callback) {
      axios.get('chapter/getList',{params:queryParams(1)}).then(res=> {
        if (res.data.code == 1) {
          layer.msg(res.msg);
        } else {
          callback(res.data.data);
        }
      })
    },
    style: 'margin-top:0;'
  });
  function queryParams(page){
    whr.page = page;
    whr.limit = 120;
    return whr;
  }
  let working = false;
	  form.on('submit(submitme)', function(data){
        if(working){
          top.layer.msg("正在组卷，请不要重复提交",{icon:3})
          return false;
        }
		var values = {categoryId:categoryId};
		$(":input").each(function(i,v){
		if(v.name)
			values[v.name] = v.value;
		})
        let trs = $("tr[data-index]");
        let cdata = [];
        for(var i=0;i<trs.length;i++){
          var tr = $(trs[i]);
          var haveChild = tr.attr("data-have-child")||false;
          var chapterId = tr.find("td[data-field=id]").text().trim();
          var dataIndex = tr.attr("data-index");
          var parentId = tr.find("td[data-field=parentId]").text().trim();
          var num1 = tr.find("td[data-field=num1]").text().trim();
          var num2 = tr.find("td[data-field=num2]").text().trim();
          var num3 = tr.find("td[data-field=num3]").text().trim();
          var num4 = tr.find("td[data-field=num4]").text().trim();
          var num5 = tr.find("td[data-field=num5]").text().trim();
          var j = {dataIndex:dataIndex,haveChild:haveChild,chapterId:chapterId,parentId:parentId,
            num1:num1,num2:num2,num3:num3,num4:num4,num5:num5};
          cdata.push(j);
        }
        values.data = JSON.stringify(cdata);
		axios.post("paper/generate",values).then(ret=>{
              if(ret.data.code==1){
                  layer.alert(ret.data.msg,{icon:2});
              }else{
                  var win = getTabWin();
                  win.loadGrid('main-table');
                  top.layer.alert("组卷成功",{icon:1});
                  var index = top.layer.getFrameIndex(window.name);
                  // top.layer.close(index);
              }
          })
          return false;
      });

})
</script>
</body>
</html>

</body>
</html>